//
//  HomeControllerCell.swift
//  swift-微博
//
//  Created by chenrin on 2016/12/28.
//  Copyright © 2016年 zhuoheng. All rights reserved.
//

import UIKit
import SDWebImage

class HomeControllerCell: UITableViewCell {

    var height : CGFloat?
    
    var homeStatus : HomeStatus?{
        didSet{
            nameLabel.text = homeStatus?.user?.name
            let source = (homeStatus?.source)! as String
            timeLabel.text = (homeStatus?.created_at)! + "  来自:\(source)"
            contentLabel.text = homeStatus?.text
            contentLabel.frame.size.width = UIScreen.main.bounds.size.width - 20
            contentLabel.sizeToFit()
            nameLabel.sizeToFit()
            height = contentLabel.frame.maxY
            let x = self.nameLabel.frame.maxX + 10
            let y = self.nameLabel.frame.minY + 1
            vipImageView.frame = CGRect(x: x, y: y, width: 14, height: 14)
            footerView.frame = CGRect(x: 0, y: height! + 10, width: UIScreen.main.bounds.size.width, height: 44)
            //设置头像
            if let iconUrl = homeStatus?.user?.profile_image_url {
                
               iconView.sd_setImage(with: URL(string: iconUrl), placeholderImage: UIImage(named: "avatar_default_big"))
            }
            //认证图片
            verifiImageView.image = homeStatus?.user?.verifiedImage
            
            //会员图标
            vipImageView.image = homeStatus?.user?.mbrankImage
        }
    }
    
    // MARK:- 懒加载
    ///头像
    private lazy var iconView : UIImageView = {
        let iv = UIImageView(image: UIImage(named:"avatar_default_big"))
        iv.frame = CGRect(x: 10, y: 10, width: 40, height: 40)
        return iv
    }()
    ///认证图标
    private lazy var verifiImageView : UIImageView = {
       let imageView = UIImageView()
       let x = self.iconView.frame.maxY - 7
       let y = self.iconView.frame.maxY - 7
        imageView.frame = CGRect(x: x, y: y, width: 14, height: 14)
        return imageView
    }()
    ///昵称
    private lazy var nameLabel : UILabel = {
       let label = UILabel()
        let x = self.iconView.frame.maxX + 10
        let y = self.iconView.frame.minY
        label.frame = CGRect(x: x, y:y , width: 10, height: 15)
        label.font = UIFont.systemFont(ofSize: 14)
        label.textColor = UIColor.darkGray
        return label
    }()
    ///会员图标
   private lazy var vipImageView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "common_icon_membership"))
       
        
        return imageView
    }()
    ///时间 和 来自哪里
   private lazy var timeLabel: UILabel = {
        let label = UILabel()
        let x = self.iconView.frame.maxX + 10
        let y = self.iconView.frame.maxY - 14
        label.textColor = UIColor.darkGray
        label.font = UIFont.systemFont(ofSize: 14)
        label.frame = CGRect(x: x, y: y, width: 300, height: 14)
        return label
    }()
    ///内容
    private lazy var contentLabel: UILabel = {
        let label = UILabel()
        let x = self.iconView.frame.minX
        let y = self.iconView.frame.maxY + 10
        label.textColor = UIColor.darkGray
        label.font = UIFont.systemFont(ofSize: 15)
        label.frame = CGRect(x: x, y: y, width: UIScreen.main.bounds.size.width - 20, height: 15)
        
        label.numberOfLines = 0
        return label
    }()
    
    ///底部工具条
    private lazy var footerView:StatuesFooterView = StatuesFooterView()
    // MARK:- init 自定义一个类需要重新的方法有designated
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        contentView.addSubview(iconView)
        contentView.addSubview(verifiImageView)
        contentView.addSubview(nameLabel)
        contentView.addSubview(vipImageView)
        contentView.addSubview(timeLabel)
        contentView.addSubview(contentLabel)
        contentView.addSubview(footerView)
        footerView.backgroundColor = UIColor.lightGray
    }
}



// MARK:- 工具条,另外一个类
class StatuesFooterView: UIView {
    // MARK:- 懒加载
    ///转发按钮
    lazy var retweetBtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("转发", for: .normal)
        btn.setImage(UIImage(named:"timeline_icon_retweet"), for: .normal)
        btn.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width / 3, height: 44)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 11)
        btn.setBackgroundImage(UIImage(named:"timeline_card_bottom_background"), for: .normal)
        btn.setTitleColor(UIColor.darkGray, for: .normal)
        btn.titleEdgeInsets = UIEdgeInsets.init(top: 0, left: 10, bottom: 0, right: 0)
        return btn
    }()
    ///赞
    lazy var likeBtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("赞", for: .normal)
        btn.setImage(UIImage(named:"timeline_icon_unlike"), for: .normal)
        btn.frame = CGRect(x: UIScreen.main.bounds.size.width / 3, y: 0, width: UIScreen.main.bounds.size.width / 3, height: 44)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 11)
        btn.setBackgroundImage(UIImage(named:"timeline_card_bottom_background"), for: .normal)
        btn.setTitleColor(UIColor.darkGray, for: .normal)
         btn.titleEdgeInsets = UIEdgeInsets.init(top: 0, left: 10, bottom: 0, right: 0)
        return btn
    }()
    ///评论
    lazy var commentBtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("评论", for: .normal)
        btn.setImage(UIImage(named:"timeline_icon_comment"), for: .normal)
        btn.frame = CGRect(x: UIScreen.main.bounds.size.width / 3 * 2, y: 0, width: UIScreen.main.bounds.size.width / 3, height: 44)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 11)
        btn.setBackgroundImage(UIImage(named:"timeline_card_bottom_background"), for: .normal)
        btn.setTitleColor(UIColor.darkGray, for: .normal)
         btn.titleEdgeInsets = UIEdgeInsets.init(top: 0, left: 10, bottom: 0, right: 0)
        return btn
    }()
    
    // MARK:- 初始化
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //初始化UI
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setupUI(){
     //1.添加子控件
        addSubview(retweetBtn)
        addSubview(likeBtn)
        addSubview(commentBtn)
    }
}



